using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Brick : MonoBehaviour
{
    private SpriteRenderer sp;

    private Sprite[] states;
    private int health;

    private void Awake() {
        sp = GetComponent<SpriteRenderer>();
    }

    public void Init(int health, Sprite[] states){
        this.health = health;
        this.states = states;
        sp.sprite = states[this.health];
        gameObject.SetActive(true);
    }

    private void OnCollisionEnter2D(Collision2D other) {
        if(other.gameObject.TryGetComponent<Ball>(out var ball))
        {
            int currentAttackPower = ball.GetCurrentAttackPower();
            health -= currentAttackPower;
            if(health < 0)
            {
                gameObject.SetActive(false);
                BrickManager.Instance.SubBrick();
                EventHandler.Dispatch(Events.SpawnProp, transform);
            }
            else
                sp.sprite = states[health];

            EventHandler.Dispatch(Events.HitBrick, 100);
            AudioManager.Instance.PlayRandomSFX(AudioManager.Instance.brickSFX);
        }
    }
}
